// pages/component/homes/purchase/moneyrecharge/moneyrecharge.js
var that;
var dataManager = require('../../../../../utils/dataManager.js');
var GlobalData = require('../../../../../utils/GlobalData.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputMoney: 0,
    msg: "",
    hidden: true,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    dataManager.removeBtnObserver("moneyrecharge")
    this.setData({
      hidden: true
    })
    if (GlobalData.homeData && GlobalData.homeData.businessname) {
      this.setData({
        msg: GlobalData.homeData.businessname
      })
    }
  },
  /**输入充值的钱数 */
  onBlurMoney: function (e) {
    // console.log(e);
    var money = parseInt(e.detail.value);
    if (money == 0) {
      return "";
    }
    this.setData({
      inputMoney: e.detail.value
    })

  },
  /**确认支付 */
  goPayment: function () {
    var that = this;
    //判断money
    // this.setData({
    //   inputMoney: e.detail.value
    // })

    var money = parseInt(that.data.inputMoney);
    // console.log(money, "输入的金额");
    // console.log(money > 1);
    if (money < 0.01 || isNaN(money)) {
      app.show("请输入正确的金额。");
    }
    else {
      that.setData({
        hidden: false
      })
      dataManager.OnRequestPost({ method: "addrecharge", "token": GlobalData.token, "amount": money,"source":"SMAPP" }, that.onProcessData, false, that.onError);
    }

  },
  /**请求成功后的回调函数 */
  onProcessData: function (data) {
    var that = this
    GlobalData.order = {};
    GlobalData.order["orderno"] = data.orderno;
    GlobalData.order["money"] = this.data.inputMoney;
    GlobalData.order["id"] = data.id;
    GlobalData.order["rechargetype"] = "余额充值";
    GlobalData.order["owntype"] = false;
    if (!GlobalData.order["time"])
      GlobalData.order["time"] = dataManager.getDate();
    GlobalData.PayReason = 1;
    // this.router.navigateByUrl('payment/moneyrecharge' + GlobalData._random());
    dataManager.OnRequestPost({ method: "getpaytype", "type": 1 }, that.onProcessgetpaytype, false, that.onError);
  },
  onError: function () {
    this.setData({
      hidden: true
    })
  },
  /**获取支付类型 */
  onProcessgetpaytype: function (data) {
    var that = this
    let arr = [];
    for (let i = 0; i < data.length; i++) {
      arr[i] = {};
      arr[i] = GlobalData.clone(data[i]);
    }
    GlobalData.PayType[GlobalData.PayReason] = arr;
    // if (dataManager.isClick("payment")) {
    //   return
    // } else {
    //   dataManager.btnObserver("payment")
    that.setData({
      hidden: true
    })
    dataManager.UseNavigateTo('../../purchase/payment/payment', false)
    // }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})